<?php

namespace app\api\controller\xykeep;
use app\common\controller\Api;
use app\common\model\Card as CardModel;
use app\common\model\UserCard as UserCardModel;
use app\common\model\Benfit as BenfitModel;
use app\common\model\UserCardBenfit as UserCardBenfitModel;
use app\api\model\xykeep\User;

class Benfit extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //三张权益卡
    public function card()
    {
        $user_id = $this->auth->id;
        $data['userinfo'] = '';
        if($user_id){
            $data['userinfo'] = User::get($user_id);
        }

        $id = input('id/d','3');

        $CardModel = new CardModel();
        $card = $CardModel->where(['id'=>$id])->with('buyed')->find();
        if($card['id']==1){
            $card['content'] = $CardModel->v1();
        }elseif ($card['id']==2){
            $card['content'] = $CardModel->v2();
        }elseif ($card['id']==3){
            $card['content'] = $CardModel->v3();
        }

        $data['card'] = $card;

        $this->success('查询成功',$data);
    }

    public function index()
    {
        $user_id = $this->auth->id;

        $data['userinfo'] = '';
        if($user_id){
            $data['userinfo'] = User::get($user_id);
        }

        $cards = CardModel::where([
            'id' => ['elt',3]
        ])->with('buyed')->select();

        $benfit_model = new BenfitModel;

        //最好等级的权益卡
        $top_card_id = UserCardModel::where([
            'card_id' => ['elt',3],
            'user_id' => $user_id
        ])->order('card_id desc')->value('card_id');
        $data['top_card'] = '';
        if($top_card_id){
            $data['top_card'] = CardModel::where(['id'=>$top_card_id])->find();
        }

        //特殊服务
        $data['specails'] = [];
        foreach ($cards as $card) {
            $data['specails'][] = $benfit_model->with('buyed')->where([
                'type' => 0,
                'card_id' => $card->id,
            ])->select();
        }

        //产品专属权益
        foreach ($cards as $card) {
            $data['goods'][] = $benfit_model->with('buyed')->where([
                'type' => 1,
                'card_id' => $card->id,
            ])->select();
        }


        //心理咨询
        $data['health'] = CardModel::where([
            'id' => 4
        ])->find();


        if($data['health']){
            if($data['health']){
                $data['health']['price'] = (int)$data['health']['price'];
            }
            $data['health']['benfit'] = $benfit_model->with('buyed')->where([
                'type' => 3,
            ])->find();
        }

        //旅游权益
        foreach ($cards as $card) {
            $data['travel'][] = $benfit_model->with('buyed')->where([
                'type' => 2,
                'card_id' => $card->id,
            ])->select();
        }

        $data['cards'] = $cards;

        $this->success('查询成功',$data);
    }

    //权益详情
    public function benfit()
    {
        $user_id = $this->auth->id;
        $id = input('id/d','');
        $benfit_model = new BenfitModel;
        $data['detail'] = $benfit_model->where(['id'=>$id])->find();
        $data['record'] = UserCardBenfitModel::where([
            'user_id' => $user_id,
            'benfit_id' => $id,
            'nums' => ['gt',0]
        ])->find();
        $this->success('查询成功',$data);
    }

}